<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>图形变换</title>
    <script type="text/javascript">
        function load () {
            let c = document.getElementById('myCanvas');
            let context = c.getContext('2d');

            context.fillStyle = '#ff00ff';
            context.strokeStyle = '#0000ff';
            context.fillRect(20, 20, 100, 100);
            context.strokeRect(20, 20, 100, 100);
            context.fill();
            context.stroke();

            context.save();

            context.fillStyle = '#ff0000';
            context.strokeStyle = '#00ff00';
            context.fillRect(140, 20, 100, 100);
            context.strokeRect(140, 20, 100, 100);
            context.fill();
            context.stroke();

            context.restore();
            context.fillRect(20, 140, 50, 50);
            context.strokeRect(80, 140, 50, 50);
            context.fill();
            context.stroke();

            // 切换画笔的颜色方法
            // context.save();
            // context.restore();
        }

        window.onload  = function () {
            load();
        }
    </script>
</head>
<body>
    <canvas id="myCanvas" style="border: 1px solid #000000" width="800" height="450">
        你的浏览器不支持canvas元素！
    </canvas>
</body>
</html>